package customview.ike.com.customviewproject.utils.permission;

import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限帮助类
 */

public class PermissionHelper {
    private  Object  mObject;
    private  String Tag="PermissionHelper";
    public  int mRequestCode;
    private  List<String> mRequestPermissions=new ArrayList<>();
    private  List<String> deniedPermissions=new ArrayList<>();
    private  List<String> alwaysDenyPermission=new ArrayList<>();
    private static PermissionHelper instance;
    private PermissionHelper(Object object){
        this.mObject= object;
    }
    public static PermissionHelper getInstance(){
        return instance;
    }
    

    private PermissionCallBack permissionCallBack;
    public interface PermissionCallBack{
        /**
         * 权限申请成功
         */
        void requestPermissionSuccess();

        /**
         * 权限申请失败
         * @param FailGetPermissions 申请失败的权限
         */
        void requestPermissionFailed(List<String> FailGetPermissions);

        /**
         * 权限申请永远被拒绝（用户勾选了不在提醒）
         * @param alwaysFailPermissions 永远申请失败的权限
         */
        void requestPermissionAlwaysFailed(List<String> alwaysFailPermissions);
    }

    /**
     * 添加权限请求码
     * @param requestCode
     * @return
     */
    public PermissionHelper addRequestCode(int requestCode){
        this.mRequestCode=requestCode;
        return this;
    }

    /**
     * 添加权限请求列表
     * @param requestPermissions
     * @return
     */
    public PermissionHelper addRequestPermissions(List<String> requestPermissions){
        this.mRequestPermissions=requestPermissions;
        return this;
    }

    /**
     * 添加activity上下文
     * @param activity
     * @return
     */
    public static PermissionHelper with(Activity activity){
        instance=new PermissionHelper(activity);
        return instance;
    }
    public static PermissionHelper with(Object object){
        instance=new PermissionHelper(object);
        return instance;
    }

    /**
     * 添加Fragment上下文
     * @param fragment
     * @return
     */
    public static PermissionHelper with(Fragment fragment){
        instance=new PermissionHelper(fragment);
        return instance;
    }
    
    public PermissionHelper addCallBack(PermissionCallBack callBack) {
        this.permissionCallBack=callBack;
        return this;
    }

    /**
     * 发起权限请求
     */
    public void request(){
        //判断当前版本是不是6.0以上
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            //大于等于6.0版本先请求相关权限，再执行相关方法
            getDeniedPermissionsList();
            if (deniedPermissions.size()==0){
                //权限已经全部授予，直接执行方法
                if (permissionCallBack!=null){
                    permissionCallBack.requestPermissionSuccess();
                }
            }else {
                //继续申请相关的权限
                if (mObject instanceof Activity){
                    ActivityCompat.requestPermissions(
                            getContext(),
                            deniedPermissions.toArray(new String[deniedPermissions.size()]),
                            mRequestCode);
                }else if (mObject instanceof Fragment){
                    Fragment fragment= (Fragment) mObject;
                    fragment.requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]),
                            mRequestCode);
                }
            }
        }else {
            //小于6.0版本，反射执行相关方法
            if (permissionCallBack!=null){
                permissionCallBack.requestPermissionSuccess();
            }
        }
    }
    /**
     * 获取被拒绝的权限列表
     * @return
     */
   
    @RequiresApi(api = Build.VERSION_CODES.M)
    public  void getDeniedPermissionsList(){
        deniedPermissions.clear();
        alwaysDenyPermission.clear();
        for (String permission :mRequestPermissions){
            int code = ContextCompat.checkSelfPermission(getContext(), permission);
            if (code == PackageManager.PERMISSION_DENIED){
                deniedPermissions.add(permission);
            }
            if (!getContext().shouldShowRequestPermissionRationale(permission)){
                alwaysDenyPermission.add(permission);
            }
        }

    }

    /**
     * 获取上下文
     */
    private  Activity getContext() {
            if (mObject instanceof Activity){
                return (Activity)mObject;
            }
            if (mObject instanceof Fragment){
                return ((Fragment)mObject).getActivity();
            }

        return null;


    }

    /**
     * 处理权限申请的回调结果
     * @param requestCode
     */
    
    @RequiresApi(api = Build.VERSION_CODES.M)
    public  void handleRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {
        getDeniedPermissionsList();
        if (deniedPermissions.size()==0){
            if (permissionCallBack!=null){
                permissionCallBack.requestPermissionSuccess();
            }
        }else {
            if (alwaysDenyPermission.size()!=0){
                if (permissionCallBack!=null){
                    permissionCallBack.requestPermissionAlwaysFailed(getAlwayDenyPermissions());
                }
            }else {
                if (permissionCallBack!=null){
                    permissionCallBack.requestPermissionFailed(getDenyPermissions());
                }
            }


        }

    }
    
    /**
     * 获取被拒绝的权限列表集合
     * @return
     */
    public  List<String> getDenyPermissions(){
        return deniedPermissions;
    }

    /**
     * 获取永远被拒绝的权限集合
     * @return
     */
    public  List<String> getAlwayDenyPermissions(){

        return alwaysDenyPermission;
    }
}
